package jun.empdepmanagersystem02.controller;

import jun.empdepmanagersystem02.pojo.Dep;
import jun.empdepmanagersystem02.pojo.Result;
import jun.empdepmanagersystem02.service.DepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/depts")
public class DepController {
    @Autowired
    private DepService depService;

    @GetMapping
    //查询所有部门
    public Result selectAll() {
        return Result.success(depService.selectAll());

    }

    @DeleteMapping("/{id}")
    //根据ID删除部门
    public Result delete(@PathVariable Integer id) {
        depService.delete(id);
        return Result.success();
    }


    @PutMapping()
    //编辑部门
    public Result update(@RequestBody Dep dep) {
        log.info("提交数据：" + dep.toString());
        return depService.update(dep);

    }

    @GetMapping("/{id}")
    //根据ID获取部门信息
    public Result getById(@PathVariable Integer id) {
        Dep dep = depService.getById(id);
        return Result.success(dep);

    }

    @PostMapping
    //新增部门
    //注意 表已经设置了自增主键 不用填写ID即可 而且可以插入后返回主键ID  上述案例有体现
    public Result insert(@RequestBody Dep dep) {
        return depService.insert(dep);
    }
}
